package com.tus.lock;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

public class UploadLockManager {
    private final ConcurrentHashMap<String, ReentrantLock> locks = new ConcurrentHashMap<>();

    public ReentrantLock getLock(String uploadId) {
        return locks.computeIfAbsent(uploadId, k -> new ReentrantLock());
    }

    public void releaseLock(String uploadId) {
        ReentrantLock lock = locks.get(uploadId);
        if (lock != null && lock.getHoldCount() > 0) {
            lock.unlock();
        }
    }

    public void removeLock(String uploadId) {
        locks.remove(uploadId);
    }
}